import 'package:flutter/material.dart';

class TextFieldDemo extends StatelessWidget {

//   获取焦点到失去焦点触发顺序
//   onTextChange ""
//   onFocusChange true
//   onTextChange "1"
//   onTextChange "12"
//   onTextChange "123"
//   onFocusChange false
//   onTextChange "123"

  @override
  Widget build(BuildContext context) {
    /**
     * onTextChangeListener
     */
    TextEditingController mcontroler = new TextEditingController();
    mcontroler.addListener(() {
      print("onInputChange:${mcontroler.text}");
    });

    /**
     * onFocusChangeListener
     */
    FocusNode focusNode = FocusNode();
    focusNode.addListener(() {
      print("onFocusChange:${focusNode.hasFocus}");
    });

    var tf1 = TextField(
        focusNode: focusNode,
        autofocus: false,
        controller: mcontroler,
        decoration: InputDecoration(
            labelText: "用户名",
            hintText: "用户名或邮箱",
            prefixIcon: Icon(Icons.person)
        ),
        onChanged: (v) {
//        print("$v");
        }
    );

    return Scaffold(
        appBar: AppBar(title: Text("TextFieldDemo")),
        body: Column(
            children: <Widget>[
              tf1,
              TextField(
                  autofocus: false,
                  decoration: InputDecoration(
                      labelText: "密码",
                      hintText: "您的登录密码",
                      prefixIcon: Icon(Icons.lock)
                  ),
                  obscureText: true
              ),
              RaisedButton(
                child: Text("et1.setText(abcdefg)"),
                onPressed: () {
                  //et.setText()
                  mcontroler.value = TextEditingValue(text: "abcdefg");
                },
              ),
              /**
               * Container控制控件宽高
               */
              Container(
                width: 100,
                height: 100,
                child: RaisedButton(
                    child: Text("et1.requestFocus()"),
                    onPressed: () {
                      //view.requestFocus
                      FocusScope.of(context).requestFocus(focusNode);
                    }
                ),
              ),
              RaisedButton(
                  child: Text("et1.clearFocus()"),
                  onPressed: () {
                    //view.clearFocus
                    focusNode.unfocus();
                  }
              )
            ]
        )
    );
  }
}